package gui;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * Created by Yaksha on 1/22/14.
 */
public class Screen extends JFrame {
    FlowPanel flowPanel = null;
    BoxPanel boxPanel = null;
    BorderPanel borderPanel = null;
    GridPanel gridPanel = null;
    GridBagPanel gridBagPanel = null;


    public Screen() {
        setBounds(100, 100, 640, 480);
        setResizable(true);
        setMinimumSize(new Dimension(340, 400));
        setTitle("Java OOP");
        getContentPane().setLayout(new CardLayout(0, 0));
        setDefaultCloseOperation(WindowConstants.DO_NOTHING_ON_CLOSE);

        JMenuBar jMenuBar = new JMenuBar();
        setJMenuBar(jMenuBar);

        JMenu mnFile = new JMenu("File");
        jMenuBar.add(mnFile);

        JMenuItem mtNew = new JMenuItem("New");
        mnFile.add(mtNew);

        JMenuItem mtOpen = new JMenuItem("Open");
        mnFile.add(mtOpen);

        JMenuItem mtExit = new JMenuItem("Exit");
        mtExit.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                closeApp();
            }
        });
        mnFile.add(mtExit);

        JMenu mnEdit = new JMenu("Edit");
        jMenuBar.add(mnEdit);

        JMenuItem mtFind = new JMenuItem("Find");
        mnEdit.add(mtFind);

        JMenu mnTool = new JMenu("Tool");
        jMenuBar.add(mnTool);

        JMenuItem mtOption = new JMenuItem("Option");
        mtOption.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                TabSelDialog tabSelect = new TabSelDialog((JFrame) getRootPane().getParent());
                tabSelect.setLocationRelativeTo(getContentPane());
                tabSelect.setVisible(true);
            }
        });
        mnTool.add(mtOption);

        JMenu mnHelp = new JMenu("Help");
        jMenuBar.add(mnHelp);

        JMenuItem mtHelp = new JMenuItem("Tutorial");
        mnHelp.add(mtHelp);

        JMenuItem mtAbout = new JMenuItem("About us");
        mnHelp.add(mtAbout);

        /**
         * Add panel to the frame:
         */
        flowPanel = new FlowPanel();
        getContentPane().add(flowPanel, "Flow Panel");

        boxPanel = new BoxPanel();
        getContentPane().add(boxPanel, "Box Panel");

        borderPanel = new BorderPanel();
        getContentPane().add(borderPanel, "Border Panel");


        gridPanel = new GridPanel();
        getContentPane().add(gridPanel, "Grid Panel");

        gridBagPanel = new GridBagPanel();
        getContentPane().add(gridBagPanel, "Grid Bag Panel");

        /**
         * Set the grid bag layout as default.
         */
        ((CardLayout) getContentPane().getLayout()).show(getContentPane(), "Grid Bag Panel");
        /**
         * Setting Close Behavior for Window.
         */
        addWindowListener(new WindowAdapter() {
            @Override
            public void windowClosing(WindowEvent e) {
                closeApp();
            }
        });
    }

    public void setPanel(String pnConstrain) {
        CardLayout cl = (CardLayout) getContentPane().getLayout();
        cl.show(getContentPane(), pnConstrain);
    }

    public void closeApp() {
        int check = JOptionPane.showConfirmDialog(getContentPane(),
                "Are you sure you want to exit the application?",
                "Exit Confirm",
                JOptionPane.YES_NO_OPTION);
        if (check == JOptionPane.YES_OPTION) {
            System.exit(0);
        }
    }
}
